#define OptimisticKeyManager
//#define OleDb
//#define Oracle

using System;
using System.Collections;
using System.Reflection;
using Eldorado.Relational;
using Eldorado.Relational.RelationalSource;
using Eldorado.Relational.SqlDriver;
using Eldorado.Data;
#if OleDb || Oracle
using Eldorado.Data.OleDb;
#else
using Eldorado.Data.SqlClient;
#endif
using Eldorado.Object;
using Eldorado.Object.Relational;
using Northwind.DataAccess;
using NUnit.Framework;

namespace Northwind.NUnit
{
	public class BaseTest : Assertion
	{
		public static string ConnectionString;
		public static IObjectDomain OuvragesDomain;
		public static IObjectDomain CustomerDemographicsDomain;

		static BaseTest()
		{
			IDataSource datasource = null;
#if Oracle
			ConnectionString = "Provider=MSDAORA.1;Password=dlb;User ID=dlb;Data Source=dlb";
			datasource = new OleDbDataSource(ConnectionString);
#else
			Eldorado.Data.SqlClient.FrmSqlConnection f = new Eldorado.Data.SqlClient.FrmSqlConnection();
			f.Catalog = "Northwind";
			if (f.ShowDialog() == System.Windows.Forms.DialogResult.OK)  
			{
#if OleDb
				ConnectionString = "Provider=SQLOLEDB;Initial Catalog=" + f.Catalog + ";" + f.ConnectionString;
				datasource = new OleDbDataSource(ConnectionString);
#else
				ConnectionString = "Initial Catalog=" + f.Catalog + ";" + f.ConnectionString;
				datasource = new SqlDataSource(ConnectionString);
#endif
			}
#endif
			if (datasource == null) 
			{
				System.Diagnostics.Trace.WriteLine("cannot connect to database");
				System.Threading.Thread.CurrentThread.Abort();
			}

			IDictionary extendedProperties = new Hashtable();
			extendedProperties["DataSource"] = datasource;
#if OptimisticKeyManager
#else
			KeyManager = new PessimisticKeyManager();
			KeyManager.ExtendedProperties["OidRange"] = 10;
#endif
#if Oracle
			CustomerDemographicsDomain = new ObjectDomain(Assembly.Load("Northwind.DataAccess"), "Northwind.DataAccess.Map.Oracle.xml", extendedProperties);
#else
			CustomerDemographicsDomain = new ObjectDomain(Assembly.Load("Northwind.DataAccess"), "Northwind.DataAccess.Map.SqlServer.xml", extendedProperties);
#endif
		}

		public IObjectContext CreateContext()
		{
#if Oracle
			IDbRelationalSource source = new OleDbRelationalSource(ConnectionString, new PlSql8Driver());
#elif OleDb
			IDbRelationalSource source = new OleDbRelationalSource(ConnectionString, new TransactSqlDriver());
#else
			IDbRelationalSource source = new SqlRelationalSource(ConnectionString, new TransactSqlDriver());
#endif
			return new RelationalObjectContext(CustomerDemographicsDomain, source);
		}

		public Category CreateCategory(IObjectContext context)
		{
			Category result = (Category)context.LocalStorage.Create(typeof(Category));
			result.CategoryName = "Category " + result.GetHashCode();
			return result;
		}

		public Customer CreateCustomer(IObjectContext context, string customerId)
		{
			Customer result = (Customer)context.LocalStorage.Create(typeof(Customer), customerId);
			result.CompanyName = "Company " + result.GetHashCode();
			return result;
		}

		public CustomerDemographic CreateCustomerDemographic(IObjectContext context)
		{
			CustomerDemographic result = (CustomerDemographic)context.LocalStorage.Create(typeof(CustomerDemographic));
			string now = DateTime.Now.Ticks.ToString();
			result.CustomerTypeID = now.Substring(now.Length - 10);
			return result;
		}

		public Employee CreateEmployee(IObjectContext context)
		{
			Employee result = (Employee)context.LocalStorage.Create(typeof(Employee));
			result.FirstName = "FirstName";
			result.LastName = "LastName";
			return result;
		}

		public Orders CreateOrders(IObjectContext context)
		{
			Orders result = (Orders)context.LocalStorage.Create(typeof(Orders));
			return result;
		}

		public OrderDetail CreateOrderDetail(IObjectContext context, Orders orders, Product product)
		{
			OrderDetail result = (OrderDetail)context.LocalStorage.Create(typeof(OrderDetail), orders, product);
			result.Discount = 0.25;
			result.Quantity = 1;
			result.UnitPrice = 1.1M;
			return result;
		}

		public Product CreateProduct(IObjectContext context)
		{
			Product result = (Product)context.LocalStorage.Create(typeof(Product));
			result.ProductName = "Product " + result.GetHashCode();
			result.Discontinued = true;
			return result;
		}

		public Region CreateRegion(IObjectContext context)
		{
			Region result = (Region)context.LocalStorage.Create(typeof(Region));
			result.RegionDescription = "Description " + result.GetHashCode();
			return result;
		}

		public Shipper CreateShipper(IObjectContext context)
		{
			Shipper result = (Shipper)context.LocalStorage.Create(typeof(Shipper));
			result.CompanyName = "Company " + result.GetHashCode();
			return result;
		}

		public Supplier CreateSupplier(IObjectContext context)
		{
			Supplier result = (Supplier)context.LocalStorage.Create(typeof(Supplier));
			result.CompanyName = "Company " + result.GetHashCode();
			return result;
		}

		public Territory CreateTerritory(IObjectContext context)
		{
			Territory result = (Territory)context.LocalStorage.Create(typeof(Territory));
			string now = DateTime.Now.Ticks.ToString();
			result.TerritoryID = now;
			result.TerritoryDescription = "Description " + result.GetHashCode();
			return result;
		}
	}
}
